#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->btnEchoNormal, &QPushButton::clicked, this, &MainWindow::onBtnEchoNormalClicked);
    connect(ui->btnEchoPassword, &QPushButton::clicked, this, &MainWindow::onBtnEchoPasswordClicked);
    connect(ui->btnEchoNoEcho, &QPushButton::clicked, this, &MainWindow::onBtnEchoNoEchoClicked);
    connect(ui->btnEchoPasswordEchoOnEdit, &QPushButton::clicked, this, &MainWindow::onBtnEchoPasswordEchoOnEdit);

    connect(ui->btnAlignLeft, &QPushButton::clicked, this, &MainWindow::onBtnAlignLeftClicked);
    connect(ui->btnAlignCenter, &QPushButton::clicked, this, &MainWindow::onBtnAlignCenterClicked);
    connect(ui->btnAlignRight, &QPushButton::clicked, this, &MainWindow::onBtnAlignRightClicked);

    connect(ui->btnReadWrite, &QPushButton::clicked, this, &MainWindow::onBtnReadWriteClicked);
    connect(ui->btnReadOnly, &QPushButton::clicked, this, &MainWindow::onBtnReadOnlyClicked);
    connect(ui->btnDisabled, &QPushButton::clicked, this, &MainWindow::onBtnDisabledClicked);
    connect(ui->btnEnabled, &QPushButton::clicked, this, &MainWindow::onBtnEnabledClicked);

    connect(ui->leAdd1, &QLineEdit::textChanged, this, &MainWindow::onLeAdd1TextChanged);
    connect(ui->leAdd2, &QLineEdit::textChanged, this, &MainWindow::onLeAdd2TextChanged);

    connect(ui->leReduce1, &QLineEdit::editingFinished, this, &MainWindow::onLeReduce1EditingFinished);
    connect(ui->leReduce2, &QLineEdit::editingFinished, this, &MainWindow::onLeReduce2EditingFinished);
}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
 * 正常模式
 * @brief MainWindow::onBtnEchoNormalClicked
 */
void MainWindow::onBtnEchoNormalClicked()
{
    ui->leEcho->clear();
    ui->leEcho->setEchoMode(QLineEdit::Normal);
    ui->leEcho->setPlaceholderText("Normal");
}

/** 密码模式
 * @brief MainWindow::onBtnEchoPasswordClicked
 */
void MainWindow::onBtnEchoPasswordClicked()
{
    ui->leEcho->clear();
    ui->leEcho->setEchoMode(QLineEdit::Password);
    ui->leEcho->setPlaceholderText("Password");
}

/** 无回显
 * @brief MainWindow::onBtnEchoNoEchoClicked
 */
void MainWindow::onBtnEchoNoEchoClicked()
{
    ui->leEcho->clear();
    ui->leEcho->setEchoMode(QLineEdit::NoEcho);
    ui->leEcho->setPlaceholderText("NoEcho");
}

/** 失去焦点变密码模式
 * @brief MainWindow::onBtnEchoPasswordEchoOnEdit
 */
void MainWindow::onBtnEchoPasswordEchoOnEdit()
{
    ui->leEcho->clear();
    ui->leEcho->setEchoMode(QLineEdit::PasswordEchoOnEdit);
    ui->leEcho->setPlaceholderText("PasswordEchoOnEdit");
}

/** 左对齐
 * @brief MainWindow::onBtnAlignLeftClicked
 */
void MainWindow::onBtnAlignLeftClicked()
{
    ui->leAligment->setAlignment(Qt::AlignLeft);
}

/** 居中对齐
 * @brief MainWindow::onBtnAlignCenterClicked
 */
void MainWindow::onBtnAlignCenterClicked()
{
    ui->leAligment->setAlignment(Qt::AlignCenter);
}

/** 右对齐
 * @brief MainWindow::onBtnAlignRightClicked
 */
void MainWindow::onBtnAlignRightClicked()
{
    ui->leAligment->setAlignment(Qt::AlignRight);
}

/** 可读可写
 * @brief MainWindow::onBtnReadWriteClicked
 */
void MainWindow::onBtnReadWriteClicked()
{
    ui->leAccess->setReadOnly(false);
    ui->leAccess->setPlaceholderText("Read&Write");
}

/** 只读
 * @brief MainWindow::onBtnReadOnlyClicked
 */
void MainWindow::onBtnReadOnlyClicked()
{
    ui->leAccess->setReadOnly(true);
    ui->leAccess->setPlaceholderText("Only Read");
}

/** 禁能
 * @brief MainWindow::onBtnDisabledClicked
 */
void MainWindow::onBtnDisabledClicked()
{
    ui->leAccess->setEnabled(false);
    ui->leAccess->setPlaceholderText("Disabled");
}

/** 使能
 * @brief MainWindow::onBtnEnabledClicked
 */
void MainWindow::onBtnEnabledClicked()
{
    ui->leAccess->setEnabled(true);
    ui->leAccess->setPlaceholderText("Enable");
}

/** 监听LeAdd1的变化
 * @brief MainWindow::onLeAdd1TextChanged
 * @param text
 */
void MainWindow::onLeAdd1TextChanged(const QString &text)
{
    int sum = text.toInt() + ui->leAdd2->text().toInt();
    ui->leSum1->setText(QString::number(sum));
}

/** 监听LeAdd2的变化
 * @brief MainWindow::onLeAdd2TextChanged
 * @param text
 */
void MainWindow::onLeAdd2TextChanged(const QString &text)
{
    int sum = text.toInt() + ui->leAdd1->text().toInt();
    ui->leSum1->setText(QString::number(sum));
}

/** 减数
 * @brief MainWindow::onLeReduce1EditingFinished
 */
void MainWindow::onLeReduce1EditingFinished()
{
    int num = ui->leReduce1->text().toInt() - ui->leReduce2->text().toInt();
    ui->leReduceValue->setText(QString::number(num));
}

/** 被减数
 * @brief MainWindow::onLeReduce2EditingFinished
 */
void MainWindow::onLeReduce2EditingFinished()
{
    int num = ui->leReduce1->text().toInt() - ui->leReduce2->text().toInt();
    ui->leReduceValue->setText(QString::number(num));
}
